<?php

/**
 * 项目发布SQL
 * @package  htdocs
 * @subpackage  quanliadmin
 * @create by allen.qiang
 */
require 'module/base/issueInfoHandler.class.php';
require 'quanliadmin/app/project/action/sqlHandler.class.php';

class pa_projectSql{
    public function execute(){
        global $G_PGLOB ;
        if(isset($_REQUEST['operFlg'])){

            switch($_REQUEST['operFlg']){

                case 1:  //检查用户登录
                    $this->init();
                    break;

                case 2:  //发布SQL列表
                    $this->sqlList();
                    break;

                case 3:   //新增发布SQL
                    $this->sqlAdd();
                    break;

                case 4:   //发布SQL的详情
                    $this->sqlDetail();
                    break;

                case 5:   //查看发布的SQL
                    $this->searchSql();
                    break;

                default:
                    $G_PGLOB->smarty->assign('msg', '参数错误，请审核后再操作');
                    $G_PGLOB->smarty->display('comm/error.tpl');

            }
        }
    }

    //检查用户登录
    function init(){

        global $G_PGLOB;

        //判断用户是否登录
        $username = $G_PGLOB->sessionCache_get('username');

        if (empty($username)){

            //如果没有设置session就进入登录模版：project/userLogin.tpl
            $returnUrl = '/quanliadmin/app/project/projectSql.pscript?operFlg=2'; //登录成功后跳转地址
            header('location: /quanliadmin/app/project/issueInfo.pscript?operFlg=2&return_url='.$returnUrl);

        }else {

            //检查当前用户是否存在
            $userLogin = new entity('s_userlogin');
            $userLogin -> username = $username;

            if (!$G_PGLOB -> DBCA -> fetch($userLogin)) {

                $G_PGLOB->smarty->assign('error_msg','密码错误');
                $G_PGLOB->smarty->display('comm/errorDisplay.tpl');

            }else {

                $this->sqlList();

            }

        }

    }

    //发布SQL列表
    function sqlList(){

        global $G_PGLOB;

        //当前用户
        $username = $G_PGLOB -> sessionCache_get('username');

        //实例化SQL管理类
        $sqlHandler = new sqlHandler( $G_PGLOB->DBCA );

        $params = array();

        //获取项目信息
        $projectList = $sqlHandler->searchProjectList($params);

        $G_PGLOB->smarty->assign( 'currentUser' , $username );
        $G_PGLOB->smarty->assign( 'projectList' , $projectList );
        $G_PGLOB->smarty->display('project/projectSqlList.tpl');

    }

    //添加发布SQL
    function sqlAdd(){

        global $G_PGLOB;

        //当前用户
        $username = $G_PGLOB -> sessionCache_get('username');

        //实例化SQL管理类
        $sqlHandler = new sqlHandler( $G_PGLOB->DBCA );

        $params = array();

        //获取项目信息
        $projectList = $sqlHandler->searchProjectList($params);

        $G_PGLOB->smarty->assign( 'currentUser' , $username );
        $G_PGLOB->smarty->assign( 'projectList' , $projectList );
        $G_PGLOB->smarty->display('project/projectSqlAdd.tpl');
    }

    //发布的SQL详情
    function sqlDetail(){

        global $G_PGLOB;

        //接收参数
        $seqno = isset( $_REQUEST['seqno'] ) ? $_REQUEST['seqno'] : '';

        if( empty($seqno) ){
            $G_PGLOB->smarty->assign('msg', '参数错误');
            $G_PGLOB->smarty->display('comm/error.tpl');
            die();
        }

        //实例化SQL发布管理类
        $sqlHandler = new sqlHandler( $G_PGLOB->DBCA );

        //查询发布的信息
        $detail = $sqlHandler->getSqlPubDetail( $_REQUEST );

        if( empty($detail) ){
            $G_PGLOB->smarty->assign('msg', '没有找到信息');
            $G_PGLOB->smarty->display('comm/error.tpl');
            die();
        }

        $G_PGLOB->smarty->assign( 'base' , $detail[0] );
        $G_PGLOB->smarty->assign( 'detail' , $detail );
        $G_PGLOB->smarty->display('project/projectSqldetail.tpl');

    }

    //查看发布的SQL
    function searchSql(){

        global $G_PGLOB;

        //接收参数
        $seqno = isset( $_REQUEST['seqno'] ) ? $_REQUEST['seqno'] : '';

        if( empty($seqno) ){
            $G_PGLOB->smarty->assign('msg', '参数错误');
            $G_PGLOB->smarty->display('comm/error.tpl');
            die();
        }

        //实例化SQL发布管理类
        $sqlHandler = new sqlHandler( $G_PGLOB->DBCA );

        //查询发布的SQL信息
        $sqlPublicInfo = $sqlHandler->getSqlList($_REQUEST);

        $G_PGLOB->smarty->assign( 'sqlPublicInfo' , $sqlPublicInfo[0] );
        $G_PGLOB->smarty->display('project/searchSqlDetail.tpl');

    }


}

